<template>
  <div class="register">
    <el-form ref="form" label-width="80px" label-position="right" :model="formItem" :rules="rules">
      <div class="login-logo">注册</div>
      <el-form-item prop="userName" label="用户名">
        <el-input v-model="formItem.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="email" label="邮箱">
        <el-input v-model="formItem.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item prop="emailcode" label="验证码">
        <el-input style="width: calc(100% - 116px);" v-model="formItem.emailcode" placeholder="请输入邮箱验证码"></el-input>
        <div class="captcha" style="width: 100px;float: right;height: 40px;">
          <el-button style="width: 100%;" type="primary" :disabled="disableClick" @click.native.prevent="sendEmailCaptcha">{{sendText}}</el-button>
        </div>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" v-model="formItem.password" placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item prop="repassword" label="确认密码">
        <el-input type="password" v-model="formItem.repassword" placeholder="请再次输入密码"></el-input>
      </el-form-item>
      <div style="text-align: center;">
        <el-button style="width: 75%;" type="primary" @click.native.prevent="register">注册</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import md5 from 'md5'

export default {
  data () {
    return {
      formItem: {
        userName: '',
        email: '',
        password: '',
        repassword: ''
      },
      sendText: '发送',
      timerCount: 0,
      disableClick: false
    }
  },
  created () {
    this.rules = {
      userName: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
      ],
      emailcode: [
        { required: true, message: '请输入邮箱验证码', trigger: 'blur' }
      ],
      password: [
        { required: true, pattern: /^[\w]{6,12}$/g, message: '请输入密码, 6-12位数字字母下划线', trigger: 'blur' }
      ],
      repassword: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        {
          validator: (rule, val, callback) => {
            if (val !== this.formItem.password) {
              callback(new Error('两次输入不一致'))
            }
            callback()
          }
        }
      ]
    }
  },
  methods: {
    register () {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return
        const params = {
          userName: this.formItem.userName,
          email: this.formItem.email,
          password: md5(this.formItem.password + 'lw'),
          captcha: this.formItem.emailcode
        }
        const result = await this.$axios.post('/user/register', params)
        if (result.code === 0) {
          this.$alert('注册成功', '成功', {
            closable: false,
            confirmButtonText: '去登录',
            callback: (action) => {
              if (action == 'confirm') {
                this.$router.push('/login')
              }
            }
          })
        } else {
          this.$message.error(result.message)
        }
      })
    },
    //重置验证码
    async sendEmailCaptcha () {
      if (!this.formItem.email) {
        return this.$message.error('请输入邮箱')
      }
      this.disableClick = true
      await this.$axios.get(`sendEmailCaptcha?email=${this.formItem.email}`)
      let count = 10
      this.sendText = `${count}s`
      this.timer = setInterval(() => {
        count--
        this.sendText = `${count}s`
        if (count === 0) {
          clearInterval(this.timer)
          this.sendText = '发送'
          this.disableClick = false
        }
      }, 1000)
    }
  }
}
</script>

<style lang="less">
html, body {
  background: #dddddd;
}
</style>
<style lang="less" scoped>
.register {
  width: 480px;
  margin: 0 auto;
  margin-top: 120px;
  background: #FFFFFF;
  border-radius: 8px;
  padding: 16px;
  padding-bottom: 24px;
  .login-logo {
    text-align: center;
    font-size: 28px;
    font-weight: bold;
    margin-bottom: 16px;
  }
  .white-color {
    background: #FFFFFF;
  }
  .captcha {
    width: 100px;
    float: right;
    height: 40px;
  }
}
</style>
